﻿using System;
using System.Windows;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Collections.Specialized;
using System.Linq;
using System.Windows.Input;
using System.ComponentModel;
using Xceed.Wpf.Toolkit.PropertyGrid.Attributes;

namespace InteControlSys.ViewModel
{
    [Serializable]
    public abstract class NodeViewModel : INPCBase, ISelectable, ICustomTypeDescriptor
    {
        public MainWindowViewModel RootVM { get; set; }
        public ProcessViewModel Owner;
        public NodeViewModel Parent { get; set; }
        
        public String NodeType { get; set; }

        public bool IsDraggable
        {
            get
            {
                return Parent != null;
            }
        }

        public void Detach()
        {
            //this.Parent.Children.Remove(this);
            //this.Parent = null;
        }
        protected ObservableCollection<NodeViewModel> _children;
        public ObservableCollection<NodeViewModel> Children
        {
            get
            {
                if (_children == null)
                {
                    _children = new ObservableCollection<NodeViewModel>();
                    _children.CollectionChanged += new NotifyCollectionChangedEventHandler(OnMoreStuffChanged);
                }

                return _children;
            }
        }
        public int TruckCount
        {
            get;
            set;
        }

        public int StatusID { get; set; }
        public int ChildrenCount
        {
            get
            {
                return this._children.Count;
                //return this.ListViewChildren.Count();
            }
            set { }
        }
        //暂时解决tree上有的根节点下不显示子节点，但又要在listview中显示
        protected ObservableCollection<NodeViewModel> _listviewchildren;
        public ObservableCollection<NodeViewModel> ListViewChildren
        {
            get { return _listviewchildren; }
            set
            {
                _listviewchildren = value;
                this.NotifyChanged("ListViewChildren");
            }
        }
        protected ObservableCollection<NodeViewModel> operatingTruckList;
        public ObservableCollection<NodeViewModel> OperatingTruckList
        {
            get { return operatingTruckList; }
            set
            {
                operatingTruckList = value;
                this.NotifyChanged("OperatingTruckList");
            }
        }
        private void OnMoreStuffChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                NodeViewModel stuff = (NodeViewModel)e.NewItems[0];
                //stuff.Parent = this;              
            }
            else if (e.Action == NotifyCollectionChangedAction.Remove)
            {
                NodeViewModel stuff = (NodeViewModel)e.OldItems[0];
                if (stuff.Parent == this)
                {
                    stuff.Parent = null;
                }
            }
        }

        private string name;
        [Category("基本")]
        [DisplayName("名称")]
        [PropertyOrder(0)]
        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                this.NotifyChanged("Name");
            }
        }
        protected Uri myIcon;
        public abstract Uri GetMyIcon();
        public virtual void UpdataIcon() { }
        public Uri MyIcon
        {
            get { return myIcon ?? (myIcon = GetMyIcon()); }
            set { myIcon = value; NotifyChanged("MyIcon"); }
        }
        
        private bool isExpanded;
        public bool IsExpanded
        {
            get
            {
                return this.isExpanded;
            }
            set
            {
                if (isExpanded == value) return;
                this.isExpanded = value;
                UpdataIcon();
                NotifyChanged("IsExpanded");
            }
        }

        private bool _isSelected;
        public virtual bool getSelectedValue() { return _isSelected; }
        public virtual void setSelectedValue(bool flag) { _isSelected = flag; }
        public bool IsSelected
        {
            get
            {
                //return this._isSelected;
                return getSelectedValue();
            }
            set
            {
                //if (_isSelected == value) return;
                //this._isSelected = value;
                setSelectedValue(value);
                NotifyChanged("IsSelected");
            }
        }

        public int Level { get; set; }

        private bool isEditing;

        public bool IsEditing
        {
            get
            {
                return this.isEditing;
            }
            set
            {
                this.isEditing = value; NotifyChanged("IsEditing");
                Debug.WriteLine(Name + ".IsEditing = " + value);
            }
        }

        public NodeViewModel()
        {

        }

        public void ExpandParents()
        {
            if (this.Parent != null)
            {
                this.Parent.ExpandParents();
                this.Parent.IsExpanded = true;
            }
        }

        public void ExpandAll()
        {
            this.IsExpanded = true;
            foreach (var child in this.Children)
            {
                child.ExpandAll();
            }
        }
        #region ICustomTypeDescriptor Members
        public AttributeCollection GetAttributes()
        {
            return TypeDescriptor.GetAttributes(this, true);
        }
        public string GetClassName()
        {
            return TypeDescriptor.GetClassName(this, true);
        }
        public string GetComponentName()
        {
            return null;
        }
        public TypeConverter GetConverter()
        {
            return TypeDescriptor.GetConverter(this, true);
        }
        public EventDescriptor GetDefaultEvent()
        {
            return TypeDescriptor.GetDefaultEvent(this, true);
        }
        public PropertyDescriptor GetDefaultProperty()
        {
            return TypeDescriptor.GetDefaultProperty(this, true);
        }
        public object GetEditor(Type editorBaseType)
        {
            return null;
        }
        public EventDescriptorCollection GetEvents(Attribute[] attributes)
        {
            return TypeDescriptor.GetEvents(this, attributes, true);
        }
        public EventDescriptorCollection GetEvents()
        {
            return TypeDescriptor.GetEvents(this, true);
        }
        public object GetPropertyOwner(PropertyDescriptor pd)
        {
            return this;
        }
        public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            return this.GetProperties();
        }

        // Method implemented to expose Volume and PayLoad properties conditionally, depending on TypeOfCar
        public virtual PropertyDescriptorCollection GetProperties()
        {
            var props = new PropertyDescriptorCollection(null);

            foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(this, true))
            {
                if (prop.Category == "基本")
                {
                    props.Add(prop);
                }
            }

            return props;
        }

        #endregion
#region Commands

        private SimpleCommand clickCommand { get; set; }
        public SimpleCommand ClickCommand
        {
            get { return clickCommand ?? (clickCommand = new SimpleCommand(ExecuteClickCommand)); }
        }
        private SimpleCommand confirmCommand { get; set; }
        public SimpleCommand ConfirmCommand
        {
            get { return confirmCommand ?? (confirmCommand = new SimpleCommand(ExecuteConfirmCommand)); }
        }
        private SimpleCommand checkBoxCommand;
        public SimpleCommand CheckBoxCommand
        {
            get { return checkBoxCommand ?? (checkBoxCommand = new SimpleCommand(ExecuteCheckBoxCommand)); }
        }
        private SimpleCommand preStepCommand { get; set; }
        public SimpleCommand PreStepCommand
        {
            get { return preStepCommand ?? (preStepCommand = new SimpleCommand(ExecutePreStepCommand)); }
        }
        private SimpleCommand nextStepCommand { get; set; }
        public SimpleCommand NextStepCommand
        {
            get { return nextStepCommand ?? (nextStepCommand = new SimpleCommand(ExecuteNextStepCommand)); }
        }
        private SimpleCommand editTruckInfoCommand { get; set; }
        public SimpleCommand EditTruckInfoCommand
        {
            get { return editTruckInfoCommand ?? (editTruckInfoCommand = new SimpleCommand(ExecuteEditTruckInfoCommand)); }
        }
        public virtual void ExecuteConfirmCommand(object parameter) { }
        public virtual void ExecuteCheckBoxCommand(object parameter)
        {
            foreach(TruckNodeViewModel item in this.OperatingTruckList)
            {
                item.IsSelected = (bool)parameter;
            }
        }
        public virtual void ExecuteClickCommand(object parameter)
        {
            //MessageBox.Show("12345");
            this.RootVM.CurrentSelectedObject = parameter as TruckNodeViewModel;
        }
        public virtual void ExecutePreStepCommand(object parameter)
        {
            //调用的流程撤销接口
        }
        public virtual void ExecuteNextStepCommand(object parameter)
        {
            //调用的流程确认下发接口
        }
        public virtual void ExecuteEditTruckInfoCommand(object parameter)
        {
            //if(parameter != null)
            //{
            //MessageBox.Show("编辑车牌");
            //}

            this.RootVM.IsEditWindowVisible = true;


        }
#endregion
    }



}